package com.nimitz.web.controller.notice;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.notice.domain.DictData;
import com.nimitz.notice.domain.Notice;
import com.nimitz.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/smart/notice")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;

    // 查看公告
    @GetMapping("/list")
    public TableDataInfo listNotice(Notice notice) {
        startPage();
        List<Notice> notices = noticeService.getNoticesForPagination(notice);
        return getDataTable(notices);
    }
  //通过Id获取
    @GetMapping("/get")
    public AjaxResult getNoticeById(@RequestParam Long noticeId) {
        Notice notice = noticeService.selectNoticeById(noticeId);
        return AjaxResult.success(notice);
    }
    // 添加公告
    @PostMapping("/add")
    public AjaxResult addNotice(@RequestBody Notice notice) {
        Long userId = SecurityUtils.getUserId();
        notice.setCreateBy(String.valueOf(userId));
        notice.setUpdateBy(String.valueOf(userId));
        noticeService.addNotice(notice);
        return AjaxResult.success("操作成功");
    }

    // 修改公告
    @PutMapping("/update")
    public AjaxResult updateNotice(@RequestBody Notice notice) {
        noticeService.updateNotice(notice);
        return AjaxResult.success("修改成功");
    }

    // 删除公告
    @DeleteMapping("/delete")
    public AjaxResult deleteNotice(@RequestParam Long noticeId) {
        noticeService.deleteNotice(noticeId);
        return AjaxResult.success("删除成功");
    }

    @GetMapping("/getAllDict")
    public AjaxResult getAllDict(){
        List<DictData> dictDatas = noticeService.getAllDictData();
        return AjaxResult.success(dictDatas);
    }
}
